//
//  ONRegisterViewController.m
//  XProject
//
//  Created by Familly on 3/5/15.
//  Copyright (c) 2015 Familly. All rights reserved.
//

#import "ONRegisterViewController.h"
#import "ONMessageViewController.h"
#import "ONLoginRequestEntity.h"
#import "ONLoginFetcher.h"

@interface ONRegisterViewController ()

@end

@implementation ONRegisterViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
//    NSString * str=[[NSUserDefaults standardUserDefaults] stringForKey:@"StokenID"];
//    if(str==nil)
//    {
//        @autoreleasepool {
//            ONAlertView * alertView=[[ONAlertView alloc] initWithFrame:[[UIScreen mainScreen] bounds] andText:@"please enable   pushnotification "];
//            [self.view addSubview:alertView];
//        
//        }
//    
//        return;
//    }
    
    
//    NSString * registerSuccess=[[NSUserDefaults standardUserDefaults] stringForKey:@"isRegisterSuccess"];
//    if(registerSuccess!=nil)
//    {
        ONGetDataRequestEntity *requestEntity = [ONGetDataRequestEntity new];
        requestEntity.token = @"asgsdgf54647fthsdfgsdfgsd";//[[NSUserDefaults standardUserDefaults] stringForKey:@"StokenID"];
        ONLoginFetcher *loginFetcher = [ONLoginFetcher new];
        [loginFetcher requestGetDataWithEntity:requestEntity success:^(ONGetDataResponseEntity *responseEntity,NSMutableArray *messageList, NSError *error){
        
            if (responseEntity.result) {
            
                [ONGlobal setMessageArray:messageList];
                [self.navigationController pushViewController:[ONMessageViewController sharedInstance] animated:true];
            
            }
            else{
                @autoreleasepool {
                    ONAlertView * alertView=[[ONAlertView alloc] initWithFrame:[[UIScreen mainScreen] bounds] andText:[NSString stringWithFormat:@"%@",@"User has not been activated - please register "]];
                    [self.view addSubview:alertView];
                
                }
            }
        
        
        } failure:^(NSError *error) {
            @autoreleasepool {
                ONAlertView * alertView=[[ONAlertView alloc] initWithFrame:[[UIScreen mainScreen] bounds] andText:@"Connect server Fail"];
                [self.view addSubview:alertView];
            
            }
        }];

    
    
   // }
    _isAgree=YES;
    UITapGestureRecognizer *tapRec = [[UITapGestureRecognizer alloc]
                                      initWithTarget:self action:@selector(tap:)];
    [self.view addGestureRecognizer: tapRec];
    _textPassword.delegate=self;
    _textUserName.delegate=self;
    // Do any additional setup after loading the view.
    [self initUI];
 
}
- (void)initUI
{
    [_lbRegister setTextColor:TEXT_TITLE_COLOR];
    [_lbBrandName setTextColor:COLOR_OF_TEXT];
    [_lbPassWord setTextColor:COLOR_OF_TEXT];
    [_btnAgree.titleLabel setTextColor:COLOR_OF_TEXT];
    [_btnForgetUserName.titleLabel setTextColor:COLOR_OF_TEXT];
    
}

- (IBAction)agreeClick:(id)sender {
    _isAgree=!_isAgree;
    [ _btnAgree setImage:(_isAgree)?[UIImage imageNamed:@"checkbox_ticked.png"]:[UIImage imageNamed:@"checkbox_not_ticked.png"] forState:UIControlStateNormal];
    
}
- (IBAction)forgetUserPassClick:(id)sender {
}
- (IBAction)submitClick:(id)sender {
    
    if([_textUserName.text isEqualToString:@""]||[_textPassword.text isEqualToString:@""])
    {
        @autoreleasepool {
            ONAlertView * alertView=[[ONAlertView alloc] initWithFrame:[[UIScreen mainScreen] bounds] andText:@"Please input your text"];
            [self.view addSubview:alertView];
            
        }
        return;
        
    }
    else{
    // check email format
        if(![self NSStringIsValidEmail:_textUserName.text])
        {
        
            @autoreleasepool {
                ONAlertView * alertView=[[ONAlertView alloc] initWithFrame:[[UIScreen mainScreen] bounds] andText:@"Your email is invalid."];
                [self.view addSubview:alertView];
            
            }
        
            return;
        }
        if(!_isAgree)
        {
            
            @autoreleasepool {
                ONAlertView * alertView=[[ONAlertView alloc] initWithFrame:[[UIScreen mainScreen] bounds] andText:@"Please accept term"];
                [self.view addSubview:alertView];
                
            }
            
            return;
        }
        NSString * stokenID = [[NSUserDefaults standardUserDefaults] stringForKey:@"StokenID"];
        if(stokenID==nil)
        {
            @autoreleasepool {
                ONAlertView * alertView=[[ONAlertView alloc] initWithFrame:[[UIScreen mainScreen] bounds] andText:@"Please enable push notification for this app"];
                [self.view addSubview:alertView];
                
            }
            
            return;
        }
        
    }
    
    ONLoginRequestEntity *requestEntity = [ONLoginRequestEntity new];
    requestEntity.email = _textUserName.text;
    requestEntity.brand = _textPassword.text;
    requestEntity.token = [[NSUserDefaults standardUserDefaults] stringForKey:@"StokenID"];
    ONLoginFetcher *loginFetcher = [ONLoginFetcher new];
    [loginFetcher requestLoginWithEntity:requestEntity success:^(ONLoginResponseEntity *responseEntity, NSError *error) {
        
        if (responseEntity.result) {
            
            NSString * registerSuccess=[[NSUserDefaults standardUserDefaults] stringForKey:@"isRegisterSuccess"];
            if(registerSuccess==nil)
            {
                
                [[NSUserDefaults standardUserDefaults] setObject:@"isRegisterSuccess" forKey:@"isRegisterSuccess"];
                @autoreleasepool {
                    ONAlertView * alertView=[[ONAlertView alloc] initWithFrame:[[UIScreen mainScreen] bounds] andText:[NSString stringWithFormat:@"%@",responseEntity.msg]];
                    [self.view addSubview:alertView];
                    
                }
            }
            else{
            
                [self.navigationController pushViewController:[ONMessageViewController sharedInstance] animated:true];
            }

        }
        else{
            @autoreleasepool {
                ONAlertView * alertView=[[ONAlertView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];;
                [self.view addSubview:alertView];
                
            }
        }
        
        
    } failure:^(NSError *error) {
        @autoreleasepool {
            ONAlertView * alertView=[[ONAlertView alloc] initWithFrame:[[UIScreen mainScreen] bounds] andText:@"Connect server Fail"];
            [self.view addSubview:alertView];
            
        }
    }];

}
-(BOOL) NSStringIsValidEmail:(NSString *)checkString
{
    BOOL stricterFilter = YES;
    NSString *stricterFilterString = @"[A-Z0-9a-z\\._%+-]+@([A-Za-z0-9-]+\\.)+[A-Za-z]{2,4}";
    NSString *laxString = @".+@([A-Za-z0-9]+\\.)+[A-Za-z]{2}[A-Za-z]*";
    NSString *emailRegex = stricterFilter ? stricterFilterString : laxString;
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:checkString];
}
/**
 * Should Return text Field   .
 */
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    
   [textField resignFirstResponder];
    return YES;
}
/**
 * Tap to Screen   .
 */
-(void)tap:(UITapGestureRecognizer *)tapRec{
    [[self view] endEditing: YES];
    [UIView animateWithDuration:0.28 animations:^{
        CGRect frame = self.view.frame;
        frame.origin.y = 0;
        self.view.frame = frame;
    }];
}
@end
